<?php
/**
 * 会员操作状态
 * @return
 * @author yuncran@126.com
 */

namespace app\api\controller;

use app\BaseController;
use app\CacheKey;
use app\FieldKey;
use app\model\BuyGiftLogModel;
use app\model\FriendModel;
use app\model\GiftModel;
use app\model\GoldLangPackageModel;
use app\model\GoldLogModel;
use app\model\GoldModel;
use app\model\GoldUserLogModel;
use app\model\QuickLanguageModel;
use app\model\RechargeModel;
use app\model\TrendsCommentModel;
use app\model\TrendsMagModel;
use app\model\TrendsModel;
use app\model\TrendsUserLikeModel;
use app\model\UserBlacklistModel;
use app\model\UserCensusCountModel;
use app\model\UserFollowModel;
use app\model\UserGiveModel;
use app\model\UserInviteModel;
use app\model\UserLevelModel;
use app\model\UserModel;
use app\model\UserMoneyLogModel;
use app\model\UserUpgradeModel;
use app\services\SendMessageServices;
use app\services\UserService;
use think\App;
use think\Exception;
use think\facade\Db;


class User extends BaseController
{

    use \app\traits\PrivacyCheckAuthTrait;

    /**
     * 加载
     * @param App $app
     */
    public function __construct(App $app)
    {
        //鉴权会员权限
        $this->init($app);
        parent::__construct($app);
    }

    /**
     * 会员隐私设置
     * @return void
     * @author yuncran@126.com
     */
    public function userSetPrivacy()
    {
        $param = $this->request->param();
        $user = UserModel::find($this->uid);
        $user->updatetime = time();
        if (!$user->save($param)) {
            return $this->renderError(lang('设置失败'));
        }
        return $this->renderSuccess(lang('设置成功'));
    }

    /**
     * 会员评论
     * @return \think\response\Json
     * @author yuncran@126.com
     */
    public function addUserTrendsComment()
    {
        $param = $this->request->param();
        $message = array(
            'trends_id.require' => lang('评论trends_id传参必填'),
            'content.require' => lang('评论content内容参数必填'),
            'content.max' => lang('评论content内容参不能超过10000个字符'),
        );

        $validate = new \think\Validate();
        $check = $validate->message($message)->check($param, ['trends_id' => 'require', 'content' => 'require|max:10000',]);
        if (!$check) return $this->renderError($validate->getError());
        $this->startTrans();
        try {
            $param['user_id'] = $this->uid;
            $param['content'] = UserService::getFilter($param['content']);
            $res = TrendsCommentModel::create($param);
            //增加评论条数
            $res1 = TrendsModel::addTrendsInc($this->uid, $param['trends_id'], 'comment_number');
            if (!$res && !$res1) return $this->renderError(lang('操作失败'));
            $this->commit();
        } catch (Exception $exception) {
            $this->rollback();
            return $this->renderError(lang('操作失败'));
        }
        return $this->renderSuccess(lang('评论成功'));
    }

    /**
     * 动态评论列表
     * @return void
     * @author yuncran@126.com
     */
    public function getTrendsComment()
    {
        $param = $this->request->param();
        $response = TrendsCommentModel::where('trends_id', $param['trends_id'])->where('status',1)
            ->order(['id' => 'DESC'])
            ->paginate()->each(function ($item) {
                return $item;
            });
        return $this->renderSuccess($response, lang('查询成功'));
    }

    /**
     * 会员动态点赞
     * @return \think\response\Json
     * @author yuncran@126.com
     */
    public function addGive()
    {
        $param = $this->request->param();
        $this->startTrans();
        try {
            $where = ['trends_id' => $param['trends_id'], 'user_id' => $this->uid, 'status' => 1];
            $response_trends_like = TrendsUserLikeModel::where($where)->find();
            if ($response_trends_like) {
                $trends_like = TrendsUserLikeModel::where($where)->delete();
                $trends = TrendsModel::minusTrendsDec($this->uid, $param['trends_id'], 'like_number');
            } else {
                $trends = TrendsModel::addTrendsInc($this->uid, $param['trends_id'], 'like_number');
                $trends_like = TrendsUserLikeModel::addTrendsLike($this->uid, $param['trends_id'], 1, $param['to_user_id'], 0);
            }
            if (!$trends && !$trends_like) {
                return $this->renderError(lang('点赞失败'));
            }
            $this->commit();
        } catch (\Throwable $throwable) {
            $this->rollback();
            return $this->renderError(lang('点赞失败'));
        }
        return $this->renderSuccess(lang('点赞成功'));
    }

    /**
     * 会员转发
     * @return void
     * @author yuncran@126.com
     */
    public function userForward()
    {
        $param = $this->request->param();
        $this->startTrans();
        try {
            $Trends = TrendsModel::where(['id' => $param['trends_id']])->find()->toArray();
            if (empty($Trends)) return $this->renderError(lang('未找到相关动态信息'));
            $det = TrendsUserLikeModel::where(['status' => 3])
                ->where(['trends_id' => $param['trends_id'], 'user_id' => $this->uid, 'to_user_id' => $param['to_user_id']])
                ->find();
            if (!empty($det)) return $this->renderError(lang('您不能重复分享了'));

            $Trends['user_id'] = $this->uid;
            $Trends['like_number'] = 0;
            $Trends['comment_number'] = 0;
            $Trends['share_number'] = 0;
            $Trends['collect_number'] = 0;
            $Trends['is_recommend'] = 0;
            $Trends['status'] = 0;            
            $share_number = TrendsModel::addTrendsInc($this->uid, $param['trends_id'], 'share_number');
            unset($Trends['id']);
            $res = TrendsModel::create($Trends);
            $trends_like = TrendsUserLikeModel::addTrendsLike($this->uid, $param['trends_id'], 3, $param['to_user_id'], $res->id);
            if (!$share_number && !$res && !$trends_like) return $this->renderError(lang('转发失败'));
            $this->commit();
        } catch (\Throwable $throwable) {
            $this->rollback();
            return $this->renderError(lang('转发失败'));
        }
        return $this->renderSuccess(lang('转发成功'));
    }


    /**
     * 会员收藏
     * @return \think\response\Json
     * @author yuncran@126.com
     */
    public function addCollect()
    {
        $param = $this->request->param();
        $where = ['trends_id' => $param['trends_id'], 'user_id' => $this->uid, 'status' => 4];
        $this->startTrans();
        try {
            $response_trends_like = TrendsUserLikeModel::where($where)->find();
            if ($response_trends_like) {
                $trends_like = TrendsUserLikeModel::Where($where)->delete();
                $trends = TrendsModel::minusTrendsDec($this->uid, $param['trends_id'], 'collect_number');
            } else {
                $trends = TrendsModel::addTrendsInc($this->uid, $param['trends_id'], 'collect_number');
                $trends_like = TrendsUserLikeModel::addTrendsLike($this->uid, $param['trends_id'], 4, $param['to_user_id'], 0);
            }
            if (!$trends && !$trends_like && !$trends_like) {
                return $this->renderError(lang('收藏失败'));
            }
            // ws 推送
            $this->commit();
        } catch (\Throwable $throwable) {
            $this->rollback();
            return $this->renderError($throwable->getMessage());
        }
        return $this->renderSuccess(lang('收藏成功'));
    }

    /**
     * 会员动态收藏列表 | 会员动态点赞列表
     * @return void
     * @author yuncran@126.com
     */
    public function collectList()
    {
        $param = $this->request->param(['status' => 1]);
        if (empty($param['status'])) return $this->renderError(lang('参数不能为空!'));
        $collect_list = TrendsUserLikeModel::where('user_id', $this->uid)->where(['status' => $param['status']])
            ->order(['id' => 'DESC'])
            ->paginate()->each(function ($item) {
                $trend = TrendsModel::where('id', $item['trends_id'])->field('id,content,user_id,like_number,comment_number,share_number,collect_number,image,video')->find();
                if(!empty($trend)){
                $trend['image'] = !empty($trend['image']) ? explode(',', $trend['image']) : '';
                $trend['video'] = !empty($trend['video']) ? explode(',', $trend['video']) : '';

                /** 分享的人 */
                $trends_user_like = Db::name('trends_user_like')->where('status', 3)->where('share_trends_id', $item['id'])->find();
                if (!empty($trends_user_like)) {
                    $trend['user_share'] = Db::name('user')->where('id', $trends_user_like['to_user_id'])->field('id,nickname')->find();
                } else {
                    $trend['user_share'] = '';
                }
                //是否评论
                $retWhere = [['user_id', '=', $this->uid], ['trends_id', '=', $trend['id']]];
                $trend['is_comment_status'] = empty(TrendsCommentModel::where($retWhere)->find()) ? 0 : 1;
                //是否分享
                $trend['is_share_status'] = empty(TrendsUserLikeModel::getIsStatus($this->uid, 3, $trend['id'])) ? 0 : 1;
                //是否收藏
                $trend['is_collect_status'] = empty(TrendsUserLikeModel::getIsStatus($this->uid, 4, $trend['id'])) ? 0 : 1;
                //是否点赞
                $trend['is_like_status'] = empty(TrendsUserLikeModel::getIsStatus($this->uid, 1, $trend['id'])) ? 0 : 1;

                        
                }
                $item['trends'] = $trend;       
                return $item;
            });
        return $this->renderSuccess($collect_list);
    }

    /**会员邀请
     * @return void
     * @author yuncran@126.com
     */
    public function addUserInvite()
    {
        $param = $this->request->param();
        $message = array(
            'email.email' => lang('邮箱格式不正确'),
        );
        $validate = new \think\Validate();
        $check = $validate->message($message)->check($param, ['email' => 'email',]);
        if (!$check) return $this->renderError(lang($validate->getError()));
        $user = UserModel::where('email', $param['email'])->find();
        if ($user) {
            return $this->renderError(lang('该邮箱用户已注册过了'));
        }
        $user1 = UserInviteModel::where('email', $param['email'])->find();
        if ($user1) {
            return $this->renderError(lang('该邮箱用户已注册过了'));
        }
        $data = [
            'user_id' => $this->uid,
            'to_user_id' => 0,
            'email' => $param['email'],
            'url' => !empty($param['url']) ? $param['url'] : '',
            'type' => $param['type'],
            'create_time' => date('Y-m-d H:i:s', time()),
        ];
        $user_invite_list = UserInviteModel::create($data);
        return $this->renderSuccess($user_invite_list);
    }

    /**
     * 会员邀请记录
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @author yuncran@126.com
     */
    public function getUserInvite()
    {
        $param = $this->request->param(['type' => 1,'limit'=>8]);
        $user_invite_list = UserInviteModel::where('user_id', $this->uid)->where(['status' => 1])
            ->paginate($param['limit'])->each(function ($item) {
                return $item;
            });
        return $this->renderSuccess($user_invite_list);
    }


    /**
     * 会员赠送金币|| 赠送礼物
     * $type  1=会员赠送金币 ， 2 = 会员赠送礼物
     * @return \think\response\Json
     * @author yuncran@126.com
     */
    public function userDonate()
    {
        $param = $this->request->param([
            'type' => 1, 'common_id' => '', 'to_user' => '', 'number' => '', 'money' => '',
            'give_type' => 1, 'blessing' => ''
        ]);

        if (empty($param['type'])) return $this->renderError(lang('缺少参数'));
        $this->startTrans();
        try {
            $user = UserModel::getUserInfo(['id' => $this->uid]);
            if (empty($user)) {
                return $this->renderError(lang('未找到相关数据'));
            }
            $draw_log = 0;
            if ($param['type'] == 1) {
                $user_ues_id = $param['to_user'];

                if (empty($param['money']) && empty($param['common_id'])) return $this->renderError(lang('请选择金额'));
                //查找金币
                if (!empty($param['common_id'])) {
                    $gold = RechargeModel::where(['id' => $param['common_id']])->find();
                    if (empty($gold)) {
                        return $this->renderError(lang('未找到相关信息'));
                    }
                    $money = $gold['gold_money'];
                } else {
                    $money = $param['money'];
                }

                $draw_log = $money;
                // 判断余额
                if ($user['money'] < $money) {
                    return $this->renderError(lang('您的余额不足'));
                }
                /** 减少余额 */
//                $minusUserDec = UserModel::minusUserDec($this->uid, 'money', $money);

                $to_user = UserModel::where('id', $param['to_user'])->find();
                if(!$to_user){
                    return $this->renderError(lang('未找到相关数据'));
                }
                $to_user_id = $to_user['id'];
                //操作完之后进行消息通知 并增加消息记录
                /** 增加赠送方余额 */
                $addUserInc = UserModel::addUserInc($to_user['id'], 'money', $money);
                event('UserMoney', ['user_id' => $to_user['id'], 'money' => $money, 'before' => $to_user['money'],
                    'after' => bcadd($to_user['money'], $money, 2), 'type' => 2,
                ]);
                /** 减少余额 */
                $minusUserDec = UserModel::minusUserDec($this->uid, 'money', $money);
                event('UserMoney', ['user_id' => $this->uid, 'money' => $money, 'before' => $user['money'],
                    'after' => bcsub($user['money'], $money, 2), 'type' => 2,
                ]);
                $ret = GoldUserLogModel::create([
                    'user_id' => $this->uid,
                    'merchant_id' => $this->merchantId,
                    'to_user_id' => $to_user['id'],
                    'money' => $money,
                    'create_time' => date('Y-m-d H:i:s', time()),
                ]);
                //消息
                SendMessageServices::sendMessage($this->uid, $to_user['id'], 'system_smg', $ret->id);

            } else {
                $to_user = UserModel::where('account|nickname|email', $param['to_user'])->find();
                if (empty($to_user)) {
                    return $this->renderError(lang('未找到相关数据'));
                }
                $to_user_id = $to_user['id'];

                if ($this->uid == $to_user['id']) return $this->renderError(lang('自己不能给自己赠送'));
                //查找礼物
                $gift = GiftModel::where(['id' => $param['common_id']])->find();
                if (empty($gift)) {
                    return $this->renderError(lang('未找到相关信息'));
                }
                // 判断余额
                if ($user['money'] < $gift['money']) {
                    return $this->renderError(lang('您的余额不足'));
                }
                /** 增加赠送放余额 */
                $addUserInc = UserModel::addUserInc($to_user['id'], 'money', $gift['money']);
                event('UserMoney', ['user_id' => $to_user['id'], 'money' => $gift['money'], 'before' => $to_user['money'],
                    'after' => bcadd($to_user['money'], $gift['money'], 2), 'type' => 1,
                ]);
                /** 减少余额 */
                $minusUserDec = UserModel::minusUserDec($this->uid, 'money', $gift['money']);
                event('UserMoney', ['user_id' => $this->uid, 'money' => $gift['money'], 'before' => $user['money'],
                    'after' => bcsub($user['money'], $gift['money'], 2), 'type' => 1,
                ]);
                $draw_log = bcmul($gift['money'], $param['number'], 2);
                //操作完之后进行消息通知 并增加消息记录
                $res = BuyGiftLogModel::create([
                    'user_id' => $this->uid,
                    'merchant_id' => $this->merchantId,
                    'to_user_id' => $to_user['id'],
                    'gift_id' => $param['common_id'],
                    'gift_type' => $param['give_type'],
                    'number' => $param['number'],
                    'price' => $gift['money'],
                    'money' => bcmul($gift['money'], $param['number'], 2),
                    'type' => $param['type'],
                    'remarks' => $param['blessing'],
                    'create_time' => date('Y-m-d H:i:s', time()),
                ]);
                SendMessageServices::checkUserSendMessage($this->uid, $to_user['id'], 'system_message', $res->id);
                //增加会员提成
                event('DrawLog', [
                    'merchant_id' => $this->merchantId,
                    'user_id' => $this->uid,
                    'money' => $draw_log,
                    'type' => $param['type'] == 1 ? 3 : 2,
                    'to_user_id' => $to_user_id,
                    'expend' => 'gift',
                ]);                
                
            }



            $this->commit();
        } catch (\Throwable $throwable) {
            $this->rollback();
            return $this->renderError(lang('操作失败' . $throwable->getMessage()));
        }

        return $this->renderSuccess(lang('会员赠送成功'));
    }


    /**
     * 会员升级
     * @return void
     * @author yuncran@126.com
     */
    public function userUpgrade()
    {
        $param = $this->request->param(['gold_id' => '']);
        if (empty($param['gold_id'])) return $this->renderError(lang('缺少参数'));
        $gold = GoldModel::where('id', $param['gold_id'])->find();
        if (empty($gold)) return $this->renderError(lang('未找到相关信息'));
        $user_level = UserLevelModel::where('id', $gold['type'])->find();
        if (empty($user_level)) return $this->renderError(lang('未找到相关信息'));
        $user = UserModel::find($this->uid);
        if ($user['money'] < $gold['money']) {
            return $this->renderErrorError(lang('您的余额不足'), 10001);
        }
        if ($user['level'] > $user_level['id']) {
            return $this->renderError(lang('您的等级要大于要升级的等级'));
        }
        $UserLevelModel = UserLevelModel::order(['id' => 'DESC'])->limit(1)->find();
        if ($user['level'] == $UserLevelModel['id']) {
            return $this->renderError(lang('您已是最高等级了、不能进行升级了'));
        }
        $new_time = time();
        if ($new_time < strtotime($user['expire_time'])) {
            $month = $gold['expire_time'];
            $start_expire_time = date('Y-m-d H:i:s', strtotime("+$month month"));
            $new_time = strtotime($user['expire_time']) - $new_time + strtotime($start_expire_time);
            if ($month > 0) {
                $end_expire_time = date('Y-m-d H:i:s', $new_time);
            } elseif ($month == 0) {
                $end_expire_time = date('Y-m-d H:i:s', 4091409957);
            } else {
                $end_expire_time = date('Y-m-d H:i:s', $new_time);
            }
        } else {
            $month = $gold['expire_time'];
            if ($month > 0) {
                $end_expire_time = date('Y-m-d H:i:s', strtotime("+$month month"));
            } elseif ($month == 0) {
                $end_expire_time = date('Y-m-d H:i:s', 4091409957);
            } else {
                $end_expire_time = date('Y-m-d H:i:s', $new_time);
            }
        }
        $this->startTrans();
        try {
            //扣余额
            UserModel::minusUserDec($this->uid, 'money', $gold['money']);
            //更新会员等级
            $user->level = $user_level['id'];          // 会员等级
            $user->expire_time = $end_expire_time;     // 会员到期时间   $gold['expire_time']
            $user->updatetime = time(); // 会员到期时间
            if (!$user->save()) {
                return $this->renderError(lang('升级失败'));
            }
            //会员收益记录
            event('DrawLog', [
                'merchant_id' => $this->merchantId,
                'user_id' => $this->uid,
                'money' => $gold['money'],
                'type' => 5,
                'expend' => 'expense',
            ]);
            //增加记录
            UserUpgradeModel::create([
                'user_id'=>$this->uid,
                'merchant_id'=>$this->merchantId,
                'money'=>$gold['money'],
                'level'=>$user_level['id'],
                'describe'=>$user_level['title'],
                'expire_time'=>$end_expire_time,
            ]);
            $this->commit();
        } catch (Exception $e) {
            $this->rollback();
            return $this->renderError(lang('升级失败'));
        }
        SendMessageServices::checkUserSendMessage($this->uid, 0, 'recharge', 0);
        return $this->renderSuccess(lang('升级成功'));
    }


    /**
     * 点击关注
     * @return void
     * @author yuncran@126.com
     */
    public function addFollow()
    {
        $param = $this->request->param(['to_user_id' => '']);
        if ($this->uid == $param['to_user_id']) {
            return $this->renderError(lang('会员不能自己关注自己'));
        }
        if (!empty($param['to_user_id'])) {
            //查看黑名单
            $userBlacklist = UserBlacklistModel::where(['user_id' => $param['to_user_id']])->find();
            if ($userBlacklist) return $this->renderError(lang('您未权限关注该会员信息'));
        }
        $this->startTrans();
        try {
            if (empty($param['to_user_id'])) return $this->renderError(lang('缺少参数'));
            $map = [['user_id', '=', $this->uid], ['to_user_id', '=', $param['to_user_id']]];
            $user_follow = UserFollowModel::where($map)->find();
            if (!empty($user_follow)) {
                UserCensusCountModel::minusUserCensusCountDec($param['to_user_id'], 'user_fans_count');
                $user_follow = UserFollowModel::where($map)->delete();
            } else {
                UserCensusCountModel::addUserCensusCountInc($param['to_user_id'], 'user_fans_count');
                $user_follow = UserFollowModel::create(['user_id' => $this->uid, 'to_user_id' => $param['to_user_id'], 'create_time' => date('Y-m-d H:i:s', time())]);
            }
            //通知信息
            SendMessageServices::checkUserSendMessage($this->uid, $param['to_user_id'], 'follow', 0);
            $this->commit();
        } catch (Exception $e) {
            $this->rollback();
            return $this->renderError(lang('操作失败'));
        }
        return $this->renderSuccess(lang('操作成功'), $user_follow);
    }


    /**
     * 会员点赞会员
     * @return \think\response\Json
     * @author yuncran@126.com
     */
    public function addUserGive()
    {
        $param = $this->request->param(['to_user_id' => '']);
        if (empty($param['to_user_id'])) return $this->renderError(lang('缺少参数'));
        $this->startTrans();
        try {
            $map = [['user_id', '=', $this->uid], ['to_user_id', '=', $param['to_user_id']], 'status' => 1];
            $user_give = UserGiveModel::where($map)->find();
            if (!empty($user_give)) {
                UserCensusCountModel::minusUserCensusCountDec($param['to_user_id'], 'user_collect_count');
                $user_give_result = UserGiveModel::where($map)->delete();
            } else {
                UserCensusCountModel::addUserCensusCountInc($param['to_user_id'], 'user_collect_count');
                $user_give_result = UserGiveModel::create(['user_id' => $this->uid, 'to_user_id' => $param['to_user_id'], 'create_time' => date('Y-m-d H:i:s', time())]);
            }
            $this->commit();
        } catch (\Throwable $throwable) {
            $this->rollback();
            return $this->renderError(lang('操作失败'));
        }
        return $this->renderSuccess(lang('操作成功'), $user_give_result);
    }

    /**
     * 会员点赞列表
     * @return void
     * @author yuncran@126.com
     */
    public function userGiveList()
    {
        $param = $this->request->param(['type' => '']);
        $map[] = $param['type'] == 1 ? [['user_id', '=', $this->uid], ['status', '=', 1], ['user_type', '=', 0]] : [['to_user_id', '=', $this->uid], ['status', '=', 1], ['to_user_type', '=', 0]];
        $list = TrendsUserLikeModel::where($map)
            ->order(['id' => 'DESC'])
            ->paginate()->each(function ($item) {
                $trends = TrendsModel::getTrends($item['trends_id']);
                $item['trends'] = $trends;
                return $item;
            });
        return $this->renderSuccess($list, lang('操作成功'));
    }


    /**清空点赞*/
    public function giveClear()
    {
        $param = $this->request->param();
        if (empty($param['type'])) return $this->renderError(lang('缺少参数'));
        if ($param['type'] == 2) {
            $ret = TrendsUserLikeModel::where('to_user_id', $this->uid)->find();
            if (empty($ret)) {
                return $this->renderError(lang('暂未清理内容'));
            }
            $response = TrendsUserLikeModel::where('to_user_id', $this->uid)->update(['update_time' => time(), 'to_user_type' => 1]);
            //更新收到点赞数量
        } else {
            $ret = TrendsUserLikeModel::where('user_id', $this->uid)->find();
            if (empty($ret)) {
                return $this->renderError(lang('暂未清理内容'));
            }
            $response = TrendsUserLikeModel::where('user_id', $this->uid)->update(['update_time' => time(), 'user_type' => 1]);
        }
        if (!$response) return $this->renderError(lang('操作失败'));
        return $this->renderSuccess(lang('操作成功'));
    }

    /**
     * 修改状态
     * @return void
     * @author yuncran@126.com
     */
    public function upUserRead()
    {
        $param = $this->request->param(['table' => '', 'id' => '']);
        $ret = UserService::get_is_read($param['table'], [['id', 'in', $param['id']]]);
        if (empty($ret)) return $this->renderError(lang('暂未清理内容'));
        $response = UserService::is_read($param['table'], [['id', 'in', $param['id']]]);
        if (!$response) return $this->renderError(lang('操作失败'));
        return $this->renderSuccess($response, lang('操作成功'));
    }

    /**
     * 修改状态
     * @return void
     * @author yuncran@126.com
     */
    public function allUserRead()
    {
        $param = $this->request->param(['table' => '']);
        $ret = UserService::getOn($param['table'], $this->uid);
        if (empty($ret)) return $this->renderError(lang('暂未清理内容'));
        $response = UserService::all_is_read($param['table'], $this->uid);
        if (!$response) return $this->renderError(lang('操作失败'));
        return $this->renderSuccess($response, lang('操作成功'));
    }

    /**
     * 会员点赞列表
     * @return void
     * @author yuncran@126.com
     */
    public function userFollowList()
    {
        $param = $this->request->param(['nickname' => '']);
        $list = UserFollowModel::where('user_id', $this->uid)
            ->where(function ($query) use ($param) {
                if (!empty($param['nickname'])) {
                    $user_ids = UserModel::where('nickname', 'like', "%{$param['nickname']}%")->column('id');
                    return $query->where(['to_user_id' => $user_ids]);
                }
            })
            ->paginate()->each(function ($item) {
                return $item;
            });
        return $this->renderSuccess($list, lang('操作成功'));
    }


    /**
     * 我的粉丝
     * @return void
     * @author yuncran@126.com
     */
    public function fansFollowList()
    {
        $list = UserFollowModel::with(['fans'])->where('to_user_id', $this->uid)
            ->paginate()->each(function ($item) {
                return $item;
            });
        return $this->renderSuccess($list, lang('操作成功'));
    }

    /**
     * 会员@我的
     * @return void
     * @author yuncran@126.com
     */
    public function mentionMy()
    {
        $list = TrendsMagModel::where('users', $this->uid)
            ->order(['id' => 'DESC'])
            ->paginate()->each(function ($item) {
                if ($item['type'] == 1) {
                    $item['association'] = UserModel::where('id', $item['user_id'])
                        ->append(['level_result', 'is_follow'])
                        ->withAttr('level_result', function ($value, $data) {
                            return UserLevelModel::where(['id' => $data['level']])->field(['id', field('title'), 'describe', 'image', 'money', 'expire', 'color', 'font_color', 'background_image', 'create_time'])->cache(CacheKey::level_title)->find();
                        })->field(FieldKey::user_field_has_one)->find();
                } else {
                    $item['association'] = UserModel::where('id', $item['users'])
                        ->append(['level_result', 'is_follow'])
                        ->withAttr('level_result', function ($value, $data) {
                            return UserLevelModel::where(['id' => $data['level']])->field(['id', field('title'), 'describe', 'image', 'money', 'expire', 'color', 'font_color', 'background_image', 'create_time'])->cache(CacheKey::level_title)->find();
                        })->field(FieldKey::user_field_has_one)->find();
                }
                return $item;
            });
        return $this->renderSuccess($list, lang('操作成功'));
    }

    /**
     * 暂时无用
     * 会员能 @ 出来的列表
     * @return void
     * @author yuncran@126.com
     */
    public function userTrendsRemind()
    {
        $param = $this->request->param(['nickname' => '']);
        $list = FriendModel::where('create_user', $this->uid)->whereOr('friend_user_id', $this->uid)
            ->where(function ($query) use ($param) {
                if (!empty($param['nickname'])) {
                    $user_ids = UserModel::where('nickname', 'like', "%{$param['nickname']}%")->column('id');
                    return $query->where(['friend_user_id' => $user_ids]);
                }
            })
            ->order(['friend_id' => 'DESC'])
            ->paginate()->each(function ($item) {
                return $item;
            });
        return $this->renderSuccess($list, lang('操作成功'));
    }


    /**
     * 隐私设置参数
     * @return void
     * @author yuncran@126.com
     */
    public function getSetUser()
    {
        $data = UserModel::where('id', $this->uid)->field([
            'id as user_id', 'is_comment', 'is_give', 'is_collect',
            'is_speak_me', 'is_comment_me', 'is_private', 'is_mail'
        ])->find();
        return $this->renderSuccess($data, lang('操作成功'));
    }

    /**
     * 购买语言包
     * @return void
     * @author yuncran@126.com
     */
    public function buyUserRender()
    {
        $param = $this->request->param(['id' => '']);
        if (empty($param['id'])) {
            return $this->renderError(lang('缺少参数'));
        }
        $this->startTrans();
        try {
            $goldLangPackage = GoldLangPackageModel::where('status', 1)->where('id', $param['id'])->find();
            if (empty($goldLangPackage)) {
                return $this->renderError(lang('未找到相关信息'));
            }
            $user = UserModel::where(['id' => $this->uid])->field('id,money,score')->find();
            if ($user['money'] < $goldLangPackage['money']) {
                return $this->renderError(lang('您的余额不足'));
            }

            /** 减少余额  增加语言包 */
            UserModel::minusUserDec($this->uid, 'money', $goldLangPackage['money']);
            UserModel::addUserInc($this->uid, 'render', $goldLangPackage['gold_money']);
            // 增加记录
            GoldLogModel::create([
                'user_id' => $this->uid,
                'merchant_id' => $this->merchantId,
                'order_sn' => order_no_gold(),
                'money' => $goldLangPackage['gold_money'],
                'create_time' => date('Y-m-d H:i:s', time()),
            ]);
            event('UserMoney', ['user_id' => $this->uid, 'money' => $goldLangPackage['money'], 'before' => $user['money'],
                'after' => bcsub($user['money'], $goldLangPackage['money'], 2), 'type' => 0,
            ]);
            //增加会员提成
            event('DrawLog', [
                'merchant_id' => $this->merchantId,
                'user_id' => $this->uid,
                'money' => $goldLangPackage['money'],
                'type' => 4,
                'expend' => 'expense',
            ]);
            $this->commit();
        } catch (Exception $e) {
            $this->rollback();
            return $this->renderError(lang('操作失败'));
        }
        return $this->renderSuccess([], lang('操作成功'));
    }


    /**
     * 购买记录
     * @return void
     * @author yuncran@126.com
     */
    public function buyUserRenderLog()
    {
        $list = GoldLogModel::where('user_id', $this->uid)
            ->paginate()->each(function ($item) {
                return $item;
            });
        return $this->renderSuccess($list, lang('操作成功'));
    }

    /**
     * 礼物赠送记录
     * @return void
     * @author yuncran@126.com
     */
    public function buyLog()
    {
        $list = BuyGiftLogModel::getUserGiftBuyAll($this->uid);
        return $this->renderSuccess($list, lang('操作成功'));
    }

    /**
     * 会员赠送记录
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @author yuncran@126.com
     */
    public function getGiveLog()
    {
        $list = GoldUserLogModel::where('user_id', $this->uid)->with(['user'])
            ->order(['id' => 'DESC'])
            ->paginate()->each(function ($item) {
                return $item;
            });
        return $this->renderSuccess($list, lang('操作成功'));
    }


    /**
     * 会员分享链接
     * @return void
     * @author yuncran@126.com
     */
    public function getUrl()
    {
        $url = $this->request->host() . '/web/dist/#/signin?invite=' . $this->uid;
        $h5  = $this->request->host() . '/index.html#/pages/login/sigin?invite=' . $this->uid;
        return $this->renderSuccess(['url' => $url,'h5'=>$h5]);
    }


    /**
     * @return void
     * @author yuncran@126.com
     */
    public function addQuickLanguage()
    {
        $param = $this->request->param(['content' => '']);
        if (empty($param['content'])) return $this->renderError(lang('参数不能为空'));
        $param['user_id'] = $this->uid;
        $result = QuickLanguageModel::create($param);
        if (!$result) {
            return $this->renderError(lang('操作失败'));
        }
        return $this->renderSuccess($result, lang('操作成功'));
    }

    /**
     * 获取快捷语
     * @return void
     * @author yuncran@126.com
     */
    public function getQuickLanguage()
    {
        $result = QuickLanguageModel::where('user_id', $this->uid)->order(['id' => 'DESC'])->select();
        return $this->renderSuccess($result, lang('操作成功'));
    }


    /**
     * 删除快捷语
     * @return void
     * @author yuncran@126.com
     */
    public function delQuickLanguage()
    {
        $param = $this->request->param(['id' => '']);
        if (empty($param['id'])) return $this->renderError(lang('参数不能为空'));
        $result = QuickLanguageModel::where(['id' => $param['id']])->delete();
        if (!$result) {
            return $this->renderError(lang('操作失败'));
        }
        return $this->renderSuccess($result, lang('操作成功'));
    }


}